package web

import (
	"gitee.com/go-boot/boot-http/binding"
	"gitee.com/go-boot/boot-utils"
	"mime/multipart"
	"net/http"
)

// RequestBody 请求的 body
type RequestBody struct {
	request                           *http.Request
	enableDecoderUserNumber           bool // 防止浮点数据精度丢失
	enableDecoderDisableUnknownFields bool // 是否忽略无法匹配的字段
}

func NewRequestBody(req *http.Request) *RequestBody {
	return &RequestBody{
		request:                           req,
		enableDecoderUserNumber:           false,
		enableDecoderDisableUnknownFields: false,
	}
}

func (r *RequestBody) PostForm(key string) (value string, err error) {
	//TODO implement me
	panic("implement me")
}

func (r *RequestBody) PostFormNumber(key string) (value int, err error) {
	//TODO implement me
	panic("implement me")
}

func (r *RequestBody) PostFormFile(key string) (value *multipart.FileHeader, err error) {
	//TODO implement me
	panic("implement me")
}

func (r *RequestBody) PostFormArray(key string) (value []string, err error) {
	//TODO implement me
	panic("implement me")
}

// contentType 获取请求类型
func (r *RequestBody) contentType() string {
	return butils.FilterFlags(r.request.Header.Get("Content-Type"))
}

func (r *RequestBody) method() string {
	return r.request.Method
}

func (r *RequestBody) PostJson(obj any) error {
	// 获取请求
	return binding.Default(r.method(), r.contentType()).Bind(r.request, obj)
}
